src/Pure/Tools/codegen_thingol.ML
author haftmann
Wed, 21 Dec 2005 15:19:16 +0100
changeset 18454 6720b5010a57
parent 18441 7488d8ea61bc
child 18515 1cad5c2b2a0b
permissions -rw-r--r--
slight improvements in name handling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
     1
(*  Title:      Pure/Tools/codegen_thingol.ML
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     2
    ID:         $Id$
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     4
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     5
Intermediate language ("Thin-gol") for code extraction.
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
*)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     7
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     8
signature CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     9
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    10
  type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    11
  datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    12
      IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    13
    | IFun of itype * itype
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    14
    | IVarT of vname * sort
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    15
    | IDictT of (string * itype) list;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    16
  datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    17
      ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    18
    | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    19
  datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    20
      IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    21
    | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    22
    | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    23
    | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    24
    | IAbs of (vname * itype) * iexpr
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    25
    | ICase of iexpr * (ipat * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    26
    | IDictE of (string * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    27
    | ILookup of (string list * vname);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    28
  val mk_funs: itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    29
  val mk_apps: iexpr * iexpr list -> iexpr;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
    30
  val mk_abss: (vname * itype) list * iexpr -> iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    31
  val pretty_itype: itype -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    32
  val pretty_ipat: ipat -> Pretty.T;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    33
  val pretty_iexpr: iexpr -> Pretty.T;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    34
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    35
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    36
  val unfold_fun: itype -> itype list * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    37
  val unfold_app: iexpr -> iexpr * iexpr list;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    38
  val unfold_abs: iexpr -> (vname * itype) list * iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    39
  val unfold_let: iexpr -> (ipat * iexpr) list * iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    40
  val itype_of_iexpr: iexpr -> itype;
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    41
  val itype_of_ipat: ipat -> itype;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    42
  val ipat_of_iexpr: iexpr -> ipat;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    43
  val eq_itype: itype * itype -> bool;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    44
  val tvars_of_itypes: itype list -> string list;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    45
  val vars_of_ipats: ipat list -> string list;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    46
  val vars_of_iexprs: iexpr list -> string list;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    47
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    48
  datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    49
      Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    50
    | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
    51
    | Typesyn of (vname * string list) list * itype
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    52
    | Datatype of (vname * string list) list * (string * itype list) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    53
    | Datatypecons of string
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    54
    | Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
    55
    | Classmember of class
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    56
    | Classinst of class * (string * (vname * string list) list) * (string * string) list;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    57
  type module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    58
  type transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    59
  type 'dst transact_fin;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    60
  type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    61
  type gen_defgen = string -> transact -> (def * string list) transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    62
  val pretty_def: def -> Pretty.T;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
    63
  val pretty_module: module -> Pretty.T; 
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
    64
  val pretty_deps: module -> Pretty.T;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    65
  val empty_module: module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    66
  val get_def: module -> string -> def;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    67
  val merge_module: module * module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    68
  val partof: string list -> module -> module;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    69
  val succeed: 'a -> transact -> 'a transact_fin;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    70
  val fail: string -> transact -> 'a transact_fin;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
    71
  val gen_invoke: (string * ('src, 'dst) gen_exprgen) list -> string
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    72
    -> 'src -> transact -> 'dst * transact;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    73
  val gen_ensure_def: (string * gen_defgen) list -> string
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
    74
    -> string -> transact -> transact;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    75
  val start_transact: (transact -> 'a * transact) -> module -> 'a * module;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    76
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    77
  val class_eq: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    78
  val type_bool: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    79
  val type_pair: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    80
  val type_list: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    81
  val type_integer: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    82
  val cons_pair: string;
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    83
  val fun_eq: string;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    84
  val fun_fst: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    85
  val fun_snd: string;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    86
  val Type_integer: itype;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    87
  val Cons_true: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    88
  val Cons_false: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    89
  val Cons_pair: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    90
  val Cons_nil: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    91
  val Cons_cons: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    92
  val Fun_eq: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    93
  val Fun_not: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    94
  val Fun_and: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    95
  val Fun_or: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    96
  val Fun_if: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    97
  val Fun_fst: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    98
  val Fun_snd: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
    99
  val Fun_0: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   100
  val Fun_1: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   101
  val Fun_add: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   102
  val Fun_mult: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   103
  val Fun_minus: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   104
  val Fun_lt: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   105
  val Fun_le: iexpr;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   106
  val Fun_wfrec: iexpr;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   107
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   108
  val prims: string list;
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   109
  val invoke_eq: ('a -> transact -> itype * transact)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   110
    -> (string * (def * (string * sort) list) -> transact -> transact)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
   111
    -> 'a -> transact -> bool * transact;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   112
  val extract_defs: iexpr -> string list;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   113
  val eta_expand: (string -> int) -> module -> module;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   114
  val eta_expand_poly: module -> module;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   115
  val tupelize_cons: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   116
  val eliminate_classes: module -> module;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   117
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   118
  val debug_level : int ref;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   119
  val debug : int -> ('a -> string) -> 'a -> 'a;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   120
  val soft_exc: bool ref;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   121
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   122
  val serialize:
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   123
    ((string -> string) -> (string * def) list -> Pretty.T option)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   124
    -> (string * Pretty.T list -> Pretty.T)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   125
    -> (string -> string option)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   126
    -> string list list -> string -> module -> Pretty.T
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   127
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   128
  val get_prefix: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list * ('a list * 'a list)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   129
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   130
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   131
signature CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   132
sig
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   133
  include CODEGEN_THINGOL;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   134
  val `%% : string * itype list -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   135
  val `-> : itype * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   136
  val `--> : itype list * itype -> itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   137
  val `$ : iexpr * iexpr -> iexpr;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   138
  val `$$ : iexpr * iexpr list -> iexpr;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   139
  val `|-> : (vname * itype) * iexpr -> iexpr;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   140
  val `|--> : (vname * itype) list * iexpr -> iexpr;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   141
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   142
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   143
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   144
structure CodegenThingolOp: CODEGEN_THINGOL_OP =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   145
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   146
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   147
(** auxiliary **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   148
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   149
val debug_level = ref 0;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   150
fun debug d f x = (if d <= !debug_level then Output.debug (f x) else (); x);
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   151
val soft_exc = ref true;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   152
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   153
fun unfoldl dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   154
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   155
   of NONE => (x, [])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   156
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   157
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   158
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   159
fun unfoldr dest x =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   160
  case dest x
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   161
   of NONE => ([], x)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   162
    | SOME (x1, x2) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   163
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   164
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   165
fun map_yield f [] = ([], [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   166
  | map_yield f (x::xs) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   167
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   168
        val (y, x') = f x
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   169
        val (ys, xs') = map_yield f xs
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   170
      in (y::ys, x'::xs') end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   171
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   172
fun get_prefix eq ([], ys) = ([], ([], ys))
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   173
  | get_prefix eq (xs, []) = ([], (xs, []))
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   174
  | get_prefix eq (xs as x::xs', ys as y::ys') =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   175
      if eq (x, y) then
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   176
        let val (ps', xys'') = get_prefix eq (xs', ys')
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   177
        in (x::ps', xys'') end
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   178
      else ([], (xs, ys));
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   179
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   180
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   181
(** language core - types, pattern, expressions **)
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   182
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   183
(* language representation *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   184
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   185
infix 8 `%%;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   186
infixr 6 `->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   187
infixr 6 `-->;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   188
infix 4 `$;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   189
infix 4 `$$;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   190
infixr 5 `|->;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   191
infixr 5 `|-->;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   192
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   193
type vname = string;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   194
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   195
datatype itype =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   196
    IType of string * itype list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   197
  | IFun of itype * itype
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   198
  | IVarT of vname * sort
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   199
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   200
  | IDictT of (string * itype) list;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   201
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   202
datatype ipat =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   203
    ICons of (string * ipat list) * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   204
  | IVarP of vname * itype;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   205
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   206
datatype iexpr =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   207
    IConst of string * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   208
  | IVarE of vname * itype
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   209
  | IApp of iexpr * iexpr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   210
  | IInst of iexpr * ClassPackage.sortlookup list list
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   211
  | IAbs of (vname * itype) * iexpr
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   212
  | ICase of iexpr * (ipat * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   213
    (*ML auxiliary*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   214
  | IDictE of (string * iexpr) list
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   215
  | ILookup of (string list * vname);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   216
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   217
(*
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   218
  variable naming conventions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   219
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   220
  bare names:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   221
    variable names          v
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   222
    class names             cls
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   223
    type constructor names  tyco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   224
    datatype names          dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   225
    const names (general)   c
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   226
    constructor names       co
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   227
    class member names      m
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   228
    arbitrary name          s
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   229
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   230
  constructs:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   231
    sort                    sort
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   232
    type                    ty
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   233
    expression              e
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   234
    pattern                 p
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   235
    instance (cls, tyco)    inst
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   236
    variable (v, ty)        var
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   237
    class member (m, ty)    membr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   238
    constructors (co, tys)  constr
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   239
 *)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   240
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   241
val mk_funs = Library.foldr IFun;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   242
val mk_apps = Library.foldl IApp;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   243
val mk_abss = Library.foldr IAbs;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   244
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   245
val op `%% = IType;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   246
val op `-> = IFun;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   247
val op `$ = IApp;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   248
val op `|-> = IAbs;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   249
val op `--> = mk_funs;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   250
val op `$$ = mk_apps;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   251
val op `|--> = mk_abss;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   252
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   253
val unfold_fun = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   254
  (fn IFun t => SOME t
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   255
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   256
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   257
val unfold_app = unfoldl
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   258
  (fn IApp e => SOME e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   259
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   260
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   261
val unfold_abs = unfoldr
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   262
  (fn IAbs b => SOME b
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   263
    | _ => NONE)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   264
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   265
val unfold_let = unfoldr
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   266
  (fn ICase (e, [(p, e')]) => SOME ((p, e), e')
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   267
    | _ => NONE);
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   268
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   269
fun map_itype f_itype (IType (tyco, tys)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   270
      tyco `%% map f_itype tys
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   271
  | map_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   272
      f_itype t1 `-> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   273
  | map_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   274
      ty;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   275
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   276
fun map_ipat f_itype f_ipat (ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   277
      ICons ((c, map f_ipat ps), f_itype ty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   278
  | map_ipat _ _ (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   279
      p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   280
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   281
fun map_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   282
      f_iexpr e1 `$ f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   283
  | map_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   284
      IInst (f_iexpr e, c)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   285
  | map_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   286
      IAbs (v, f_iexpr e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   287
  | map_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   288
      ICase (f_iexpr e, map (fn (p, e) => (f_ipat p, f_iexpr e)) ps)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   289
  | map_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   290
      e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   291
  | map_iexpr _ _ _ (e as IVarE _) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   292
      e
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   293
  | map_iexpr f_itype f_ipat f_iexpr (IDictE ms) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   294
      IDictE (map (apsnd f_iexpr) ms)
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   295
  | map_iexpr _ _ _ (e as ILookup _) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   296
      e ;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   297
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   298
fun fold_itype f_itype (IFun (t1, t2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   299
      f_itype t1 #> f_itype t2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   300
  | fold_itype _ (ty as IType _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   301
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   302
  | fold_itype _ (ty as IVarT _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   303
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   304
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   305
fun fold_ipat f_itype f_ipat (ICons ((_, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   306
      f_itype ty #> fold f_ipat ps
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   307
  | fold_ipat f_itype f_ipat (p as IVarP _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   308
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   309
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   310
fun fold_iexpr f_itype f_ipat f_iexpr (IApp (e1, e2)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   311
      f_iexpr e1 #> f_iexpr e2
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   312
  | fold_iexpr f_itype f_ipat f_iexpr (IInst (e, c)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   313
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   314
  | fold_iexpr f_itype f_ipat f_iexpr (IAbs (v, e)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   315
      f_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   316
  | fold_iexpr f_itype f_ipat f_iexpr (ICase (e, ps)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   317
      f_iexpr e #> fold (fn (p, e) => f_ipat p #> f_iexpr e) ps
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   318
  | fold_iexpr _ _ _ (e as IConst _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   319
      I
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   320
  | fold_iexpr _ _ _ (e as IVarE _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   321
      I;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   322
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   323
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   324
(* simple type matching *)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   325
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   326
fun eq_itype (ty1, ty2) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   327
  let
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   328
    exception NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   329
    fun eq (IVarT (v1, sort1)) (IVarT (v2, sort2)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   330
          if sort1 <> sort2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   331
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   332
          else
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   333
            (case AList.lookup (op =) subs v1
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   334
             of NONE => subs |> AList.update (op =) (v1, v2)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   335
              | (SOME v1') =>
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   336
                  if v1' <> v2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   337
                  then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   338
                  else subs)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   339
      | eq (IType (tyco1, tys1)) (IType (tyco2, tys2)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   340
          if tyco1 <> tyco2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   341
          then raise NO_MATCH
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   342
          else subs |> fold2 eq tys1 tys2
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   343
      | eq (IFun (ty11, ty12)) (IFun (ty21, ty22)) subs =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   344
          subs |> eq ty11 ty21 |> eq ty12 ty22
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   345
      | eq _ _ _ = raise NO_MATCH;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   346
  in
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   347
    (eq ty1 ty2 []; true)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   348
    handle NO_MATCH => false
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   349
  end;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   350
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   351
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   352
(* simple diagnosis *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   353
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   354
fun pretty_itype (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   355
      Pretty.gen_list "" "(" ")" (Pretty.str tyco :: map pretty_itype tys)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   356
  | pretty_itype (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   357
      Pretty.gen_list "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   358
  | pretty_itype (IVarT (v, sort)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   359
      Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort))
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   360
  | pretty_itype (IDictT _) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   361
      Pretty.str "<DictT>";
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   362
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   363
fun pretty_ipat (ICons ((cons, ps), ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   364
      Pretty.gen_list " " "(" ")"
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   365
        (Pretty.str cons :: map pretty_ipat ps @ [Pretty.str ":: ", pretty_itype ty])
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   366
  | pretty_ipat (IVarP (v, ty)) =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   367
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty];
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   368
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   369
fun pretty_iexpr (IConst (f, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   370
      Pretty.block [Pretty.str (f ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   371
  | pretty_iexpr (IVarE (v, ty)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   372
      Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   373
  | pretty_iexpr (IApp (e1, e2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   374
      Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   375
  | pretty_iexpr (IInst (e, c)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   376
      pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   377
  | pretty_iexpr (IAbs ((v, ty), e)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   378
      Pretty.enclose "(" ")" [Pretty.str ("?" ^ v ^ " |->"), Pretty.brk 1, pretty_iexpr e]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   379
  | pretty_iexpr (ICase (e, cs)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   380
      Pretty.enclose "(" ")" [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   381
        Pretty.str "case ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   382
        pretty_iexpr e,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   383
        Pretty.enclose "(" ")" (map (fn (p, e) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   384
          Pretty.block [
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   385
            pretty_ipat p,
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   386
            Pretty.str " => ",
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   387
            pretty_iexpr e
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   388
          ]
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   389
        ) cs)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   390
      ]
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   391
  | pretty_iexpr (IDictE _) =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   392
      Pretty.str "<DictE>"
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   393
  | pretty_iexpr (ILookup (ls, v)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   394
      Pretty.str ("<Lookup: " ^ commas ls ^ " in " ^ v ^ ">");
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   395
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   396
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   397
(* language auxiliary *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   398
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   399
fun itype_of_iexpr (IConst (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   400
  | itype_of_iexpr (IVarE (_, ty)) = ty
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   401
  | itype_of_iexpr (e as IApp (e1, e2)) = (case itype_of_iexpr e1
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   402
      of (IFun (ty2, ty')) =>
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   403
            if ty2 = itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   404
            then ty'
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   405
            else error ("inconsistent application: in " ^ Pretty.output (pretty_iexpr e)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   406
              ^ ", " ^ (Pretty.output o pretty_itype) ty2 ^ " vs. " ^ (Pretty.output o pretty_itype o itype_of_iexpr) e2)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   407
       | _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1)))
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   408
  | itype_of_iexpr (IInst (e, cs)) = itype_of_iexpr e
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   409
  | itype_of_iexpr (IAbs ((_, ty1), e2)) = ty1 `-> itype_of_iexpr e2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   410
  | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   411
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   412
fun itype_of_ipat (ICons (_, ty)) = ty
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   413
  | itype_of_ipat (IVarP (_, ty)) = ty;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   414
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   415
fun ipat_of_iexpr (IConst (f, ty)) = ICons ((f, []), ty)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   416
  | ipat_of_iexpr (IVarE v) = IVarP v
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   417
  | ipat_of_iexpr (e as IApp _) =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   418
      (case unfold_app e
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   419
        of (IConst (f, ty), es) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   420
              ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   421
         | (IInst (IConst (f, ty), _), es) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   422
              ICons ((f, map ipat_of_iexpr es), (snd o unfold_fun) ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   423
         | _ => error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   424
  | ipat_of_iexpr e =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   425
      error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e);
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   426
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   427
fun tvars_of_itypes tys =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   428
  let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   429
    fun vars (IType (_, tys)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   430
          fold vars tys
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   431
      | vars (IFun (ty1, ty2)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   432
          vars ty1 #> vars ty2
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   433
      | vars (IVarT (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   434
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   435
  in fold vars tys [] end;
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
   436
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   437
fun vars_of_ipats ps =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   438
  let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   439
    fun vars (ICons ((_, ps), _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   440
          fold vars ps
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   441
      | vars (IVarP (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   442
          insert (op =) v
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   443
  in fold vars ps [] end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   444
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   445
fun vars_of_iexprs es =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   446
  let
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   447
    fun vars (IConst (f, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   448
          I
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   449
      | vars (IVarE (v, _)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   450
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   451
      | vars (IApp (e1, e2)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   452
          vars e1 #> vars e2
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   453
      | vars (IAbs ((v, _), e)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   454
          insert (op =) v
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   455
          #> vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   456
      | vars (ICase (e, cs)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   457
          vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   458
          #> fold (fn (p, e) => fold (insert (op =)) (vars_of_ipats [p]) #> vars e) cs
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   459
      | vars (IInst (e, lookup)) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   460
          vars e
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   461
      | vars (IDictE ms) =
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   462
          fold (vars o snd) ms
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   463
      | vars (ILookup (_, v)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   464
          cons v
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   465
  in fold vars es [] end;
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   466
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   467
fun instant_itype (v, sty) ty =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   468
  let
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   469
    fun instant (IType (tyco, tys)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   470
          tyco `%% map instant tys
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   471
      | instant (IFun (ty1, ty2)) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   472
          instant ty1 `-> instant ty2
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   473
      | instant (w as (IVarT (u, _))) =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   474
          if v = u then sty else w
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   475
  in instant ty end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
   476
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   477
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   478
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   479
(** language module system - definitions, modules, transactions **)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   480
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   481
(* type definitions *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   482
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   483
datatype def =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   484
    Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   485
  | Fun of (ipat list * iexpr) list * (ClassPackage.sortcontext * itype)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   486
  | Typesyn of (vname * string list) list * itype
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   487
  | Datatype of (vname * string list) list * (string * itype list) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   488
  | Datatypecons of string
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   489
  | Class of class list * vname * (string * (ClassPackage.sortcontext * itype)) list * string list
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   490
  | Classmember of class
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   491
  | Classinst of class * (string * (vname * string list) list) * (string * string) list;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   492
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   493
datatype node = Def of def | Module of node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   494
type module = node Graph.T;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   495
type transact = Graph.key list * module;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   496
datatype 'dst transact_res = Succeed of 'dst | Fail of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   497
type 'dst transact_fin = 'dst transact_res * transact;
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   498
type ('src, 'dst) gen_exprgen = 'src -> transact -> 'dst transact_fin;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   499
type gen_defgen = string -> transact -> (def * string list) transact_fin;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   500
exception FAIL of string list * exn option;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   501
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   502
val eq_def = (op =);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   503
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   504
(* simple diagnosis *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   505
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   506
fun pretty_def Nop =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   507
      Pretty.str "<NOP>"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   508
  | pretty_def (Fun (eqs, (_, ty))) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   509
      Pretty.gen_list " |" "" "" (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   510
        map (fn (ps, body) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   511
          Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   512
            Pretty.gen_list "," "[" "]" (map pretty_ipat ps),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   513
            Pretty.str " |->",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   514
            Pretty.brk 1,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   515
            pretty_iexpr body,
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   516
            Pretty.str "::",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   517
            pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   518
          ]) eqs
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   519
        )
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   520
  | pretty_def (Typesyn (vs, ty)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   521
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   522
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   523
        Pretty.str " |=> ",
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   524
        pretty_itype ty
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   525
      ]
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   526
  | pretty_def (Datatype (vs, cs, insts)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   527
      Pretty.block [
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   528
        Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   529
        Pretty.str " |=> ",
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   530
        Pretty.gen_list " |" "" ""
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   531
          (map (fn (c, tys) => (Pretty.block o Pretty.breaks) (Pretty.str c :: map pretty_itype tys)) cs),
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   532
        Pretty.str ", instances ",
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   533
        Pretty.gen_list "," "[" "]" (map Pretty.str insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   534
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   535
  | pretty_def (Datatypecons dtname) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   536
      Pretty.str ("cons " ^ dtname)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   537
  | pretty_def (Class (supcls, v, mems, insts)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   538
      Pretty.block [
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   539
        Pretty.str ("class var " ^ v ^ "extending "),
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   540
        Pretty.gen_list "," "[" "]" (map Pretty.str supcls),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   541
        Pretty.str " with ",
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   542
        Pretty.gen_list "," "[" "]"
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   543
          (map (fn (m, (_, ty)) => Pretty.block [Pretty.str (m ^ "::"), pretty_itype ty]) mems),
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   544
        Pretty.str " instances ",
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   545
        Pretty.gen_list "," "[" "]" (map Pretty.str insts)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   546
      ]
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   547
  | pretty_def (Classmember clsname) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   548
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   549
        Pretty.str "class member belonging to ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   550
        Pretty.str clsname
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   551
      ]
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   552
  | pretty_def (Classinst (clsname, (tyco, arity), mems)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   553
      Pretty.block [
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   554
        Pretty.str "class instance (",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   555
        Pretty.str clsname,
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   556
        Pretty.str ", (",
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   557
        Pretty.str tyco,
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   558
        Pretty.str ", ",
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   559
        Pretty.gen_list "," "[" "]" (map (Pretty.gen_list "," "{" "}" o map Pretty.str o snd) arity),
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   560
        Pretty.str ")) with ",
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   561
        Pretty.gen_list "," "[" "]" (map (fn (m1, m2) => Pretty.str (m1 ^ " => " ^ m2)) mems)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   562
      ];
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   563
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   564
fun pretty_module modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   565
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   566
    fun pretty (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   567
          Pretty.block (
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   568
            Pretty.str ("module " ^ name ^ " {")
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   569
            :: Pretty.brk 1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   570
            :: Pretty.chunks (map pretty (AList.make (Graph.get_node modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   571
                 (Graph.strong_conn modl |> List.concat |> rev)))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   572
            :: Pretty.str "}" :: nil
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   573
          )
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   574
      | pretty (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   575
          Pretty.block [Pretty.str name, Pretty.str " :=", Pretty.brk 1, pretty_def def]
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   576
  in pretty ("//", Module modl) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   577
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   578
fun pretty_deps modl =
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   579
  let
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   580
    fun one_node key =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   581
      let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   582
        val preds_ = Graph.imm_preds modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   583
        val succs_ = Graph.imm_succs modl key;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   584
        val mutbs = gen_inter (op =) (preds_, succs_);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   585
        val preds = fold (remove (op =)) mutbs preds_;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   586
        val succs = fold (remove (op =)) mutbs succs_;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   587
      in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   588
        (Pretty.block o Pretty.fbreaks) (
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   589
          Pretty.str key
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   590
          :: map (fn s => Pretty.str ("<-> " ^ s)) mutbs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   591
          @ map (fn s => Pretty.str ("<-- " ^ s)) preds
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   592
          @ map (fn s => Pretty.str ("--> " ^ s)) succs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   593
          @ (the_list oo Option.mapPartial) ((fn Module modl' => SOME (pretty_deps modl') | _ => NONE) o Graph.get_node modl) (SOME key)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   594
        )
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   595
      end
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   596
  in
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   597
    modl
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   598
    |> Graph.strong_conn
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   599
    |> List.concat
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   600
    |> rev
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   601
    |> map one_node
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   602
    |> Pretty.chunks
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   603
  end;
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   604
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   605
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   606
(* name handling *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   607
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   608
fun dest_name name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   609
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   610
    val name' = NameSpace.unpack name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   611
    val (name'', name_base) = split_last name'
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   612
    val (modl, shallow) = split_last name''
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   613
  in (modl, NameSpace.pack [shallow, name_base]) end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   614
  handle Empty => error ("not a qualified name: " ^ quote name);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   615
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   616
fun dest_modl (Module m) = m;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   617
fun dest_def (Def d) = d;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   618
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   619
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   620
(* modules *)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   621
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   622
val empty_module = Graph.empty; (*read: "depends on"*)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   623
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   624
fun get_def modl name =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   625
  case dest_name name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   626
   of (modlname, base) =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   627
        let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   628
          fun get (Module node) [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   629
                (dest_def o Graph.get_node node) base
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   630
            | get (Module node) (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   631
                get (Graph.get_node node m) ms
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   632
        in get (Module modl) modlname end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   633
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   634
fun add_def (name, def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   635
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   636
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   637
    fun add [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   638
          Graph.new_node (base, Def def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   639
      | add (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   640
          Graph.default_node (m, Module empty_module)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   641
          #> Graph.map_node m (Module o add ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   642
  in add modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   643
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   644
fun map_def name f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   645
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   646
    val (modl, base) = dest_name name;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   647
    fun mapp [] =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   648
          Graph.map_node base (Def o f o dest_def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   649
      | mapp (m::ms) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   650
          Graph.map_node m (Module o mapp ms o dest_modl)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   651
  in mapp modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   652
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   653
fun add_dep (name1, name2) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   654
  if name1 = name2 then modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   655
  else
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   656
    let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   657
      val m1 = dest_name name1 |> apsnd single |> (op @);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   658
      val m2 = dest_name name2 |> apsnd single |> (op @);
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   659
      val (ms, (r1, r2)) = get_prefix (op =) (m1, m2);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   660
      val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   661
      val add_edge =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   662
        if null r1 andalso null r2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   663
        then Graph.add_edge
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   664
        else Graph.add_edge_acyclic
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   665
      fun add [] node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   666
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   667
            |> add_edge (s1, s2)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   668
        | add (m::ms) node =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   669
            node
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   670
            |> Graph.map_node m (Module o add ms o dest_modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   671
    in add ms modl end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   672
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   673
fun map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   674
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   675
    fun mapp (Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   676
          (Def o f) def
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   677
      | mapp (Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   678
          (Module o Graph.map_nodes mapp) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   679
  in dest_modl o mapp o Module end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   680
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   681
fun fold_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   682
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   683
    fun fol prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   684
          f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   685
      | fol prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   686
          Graph.fold_nodes (fol (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   687
  in Graph.fold_nodes (fol []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   688
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   689
fun add_deps f modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   690
  modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   691
  |> fold add_dep ([] |> fold_defs (append o f) modl);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   692
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   693
fun fold_map_defs f =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   694
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   695
    fun foldmap prfix (name, Def def) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   696
          apfst Def o f (NameSpace.pack (prfix @ [name]), def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   697
      | foldmap prfix (name, Module modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   698
          apfst Module o Graph.fold_map_nodes (foldmap (prfix @ [name])) modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   699
  in Graph.fold_map_nodes (foldmap []) end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   700
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   701
fun map_def_fun f_ipat f_iexpr (Fun (eqs, cty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   702
      Fun (map (fn (ps, rhs) => (map f_ipat ps, f_iexpr rhs)) eqs, cty)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   703
  | map_def_fun _ _ def = def;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   704
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   705
fun transform_defs f_def f_ipat f_iexpr s modl =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   706
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   707
    val (modl', s') = fold_map_defs f_def modl s
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   708
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   709
    modl'
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   710
    |> map_defs (map_def_fun (f_ipat s') (f_iexpr s'))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   711
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   712
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   713
fun merge_module modl12 =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   714
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   715
    fun join_module (Module m1, Module m2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   716
          (SOME o Module) (merge_module (m1, m2))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   717
      | join_module (Def d1, Def d2) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   718
          if eq_def (d1, d2) then (SOME o Def) d1 else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   719
      | join_module _ =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   720
          NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   721
  in Graph.join (K join_module) modl12 end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   722
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   723
fun partof names modl =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   724
  let
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   725
    datatype pathnode = PN of (string list * (string * pathnode) list);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   726
    fun mk_ipath ([], base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   727
          PN (base :: defs, modls)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   728
      | mk_ipath (n::ns, base) (PN (defs, modls)) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   729
          modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   730
          |> AList.default (op =) (n, PN ([], []))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   731
          |> AList.map_entry (op =) n (mk_ipath (ns, base))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   732
          |> (pair defs #> PN);
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   733
    fun select (PN (defs, modls)) (Module module) =
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   734
      module
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   735
      |> Graph.subgraph (Graph.all_succs module (defs @ map fst modls))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   736
      |> fold (fn (name, modls) => Graph.map_node name (select modls)) modls
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   737
      |> Module;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   738
  in
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   739
    Module modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   740
    |> select (fold (mk_ipath o dest_name) (filter NameSpace.is_qualified names) (PN ([], [])))
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   741
    |> dest_modl
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   742
  end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   743
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   744
fun (*add_check_transform (name, (Datatypecons dtname)) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   745
      (debug 7 (fn _ => "transformation for datatype constructor " ^ quote name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   746
        ^ " of datatype " ^ quote dtname) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   747
      ([([dtname],
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   748
          fn [Datatype (_, _, [])] => NONE
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   749
            | _ => "attempted to add constructor to already instantiating datatype" |> SOME)],
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   750
       [(dtname,
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   751
          fn Datatype (vs, cs, insts) => Datatype (vs, name::cs, insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   752
           | def => "attempted to add datatype constructor to non-datatype: "
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   753
              ^ (Pretty.output o pretty_def) def |> error)])
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   754
      )
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   755
  | add_check_transform (name, Classmember (clsname, v, ty)) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   756
      let
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   757
        val _ = debug 7 (fn _ => "transformation for class member " ^ quote name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   758
        ^ " of class " ^ quote clsname) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   759
        fun check_var (IType (tyco, tys)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   760
              fold check_var tys s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   761
          | check_var (IFun (ty1, ty2)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   762
              s
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   763
              |> check_var ty1
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   764
              |> check_var ty2
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   765
          | check_var (IVarT (w, sort)) s =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   766
              if v = w
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   767
              andalso member (op =) sort clsname
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   768
              then "additional class appears at type variable" |> SOME
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   769
              else NONE
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   770
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   771
        ([([], fn [] => check_var ty NONE),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   772
          ([clsname],
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   773
             fn [Class (_, _, _, [])] => NONE
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   774
              | _ => "attempted to add class member to witnessed class" |> SOME)],
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   775
         [(clsname,
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   776
             fn Class (supcs, v, mems, insts) => Class (supcs, v, name::mems, insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   777
              | def => "attempted to add class member to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   778
                 ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   779
      end
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   780
  | *) add_check_transform (name, Classinst (clsname, (tyco, arity), memdefs)) =
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   781
      let
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   782
        val _ = debug 7 (fn _ => "transformation for class instance " ^ quote tyco
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   783
          ^ " of class " ^ quote clsname ^ " with " ^ (commas o map (fn (m1, m2) => m1 ^ " => " ^ m2)) memdefs) ();
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   784
        (* fun check [Classmember (_, v, mtyp_c), Fun (_, (_, mtyp_i))] =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   785
              let
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   786
                val mtyp_i' = instant_itype (v, tyco `%% map IVarT arity) mtyp_c;
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   787
              in if eq_itype (mtyp_i', mtyp_i)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   788
                then NONE
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   789
                else "wrong type signature for class member: "
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   790
                  ^ (Pretty.output o pretty_itype) mtyp_i' ^ " expected, "
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   791
                  ^ (Pretty.output o pretty_itype) mtyp_i ^ " given" |> SOME
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   792
              end
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   793
          | check defs =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   794
              "non-well-formed definitions encountered for classmembers: "
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   795
              ^ (commas o map (quote o Pretty.output o pretty_def)) defs |> SOME *)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   796
      in
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   797
        ((* map (fn (memname, memprim) => ([memname, memprim], check)) memdefs*) [],
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   798
          [(clsname,
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   799
              fn Class (supcs, v, mems, insts) => Class (supcs, v, mems, name::insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   800
               | def => "attempted to add class instance to non-class"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   801
                  ^ (Pretty.output o pretty_def) def |> error),
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   802
           (tyco,
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   803
              fn Datatype (vs, cs, insts) => Datatype (vs, cs, name::insts)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   804
               | Nop => Nop
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   805
               | def => "attempted to instantiate non-type to class instance"
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   806
                  ^ (Pretty.output o pretty_def) def |> error)])
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   807
      end
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   808
  | add_check_transform _ = ([], []);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   809
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   810
(* checks to be implemented here lateron:
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   811
    - well-formedness of function equations
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   812
    - only possible to add defined constructors and class members
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   813
    - right type abstraction with class members
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   814
    - correct typing of instance definitions
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   815
*)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   816
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   817
fun succeed some = pair (Succeed some);
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   818
fun fail msg = pair (Fail ([msg], NONE));
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   819
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   820
fun check_fail _ (Succeed dst, trns) = (dst, trns)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   821
  | check_fail msg (Fail (msgs, e), _) = raise FAIL (msg::msgs, e);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   822
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   823
fun select_generator _ _ [] modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   824
      ([], modl) |> fail ("no code generator available")
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   825
  | select_generator mk_msg src gens modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   826
      let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   827
        fun handle_fail msgs f =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   828
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   829
            in
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   830
              if ! soft_exc
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   831
              then
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   832
                ([], modl) |> f
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   833
                handle FAIL exc => (Fail exc, ([], modl))
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   834
                     | e => (Fail (msgs, SOME e), ([], modl))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   835
              else
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   836
                ([], modl) |> f
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   837
                handle FAIL exc => (Fail exc, ([], modl))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   838
            end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   839
        fun select msgs [(gname, gen)] =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   840
          handle_fail (msgs @ [mk_msg gname]) (gen src)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   841
        fun select msgs ((gname, gen)::gens) =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   842
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   843
            val msgs' = msgs @ [mk_msg gname]
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   844
          in case handle_fail msgs' (gen src)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   845
          of (Fail (_, NONE), _) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   846
               select msgs' gens
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   847
           | result =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   848
               result
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   849
          end;
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   850
      in select [] gens end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   851
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   852
fun gen_invoke codegens msg src (deps, modl) =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   853
  modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   854
  |> select_generator (fn gname => "trying code generator " ^ gname ^ " for source " ^ quote msg)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   855
       src codegens
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   856
  |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   857
  ||> (fn (deps', modl') => (append deps' deps, modl'));
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   858
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   859
fun gen_ensure_def defgens msg name (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   860
  let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   861
    fun add (name, def) (deps, modl) =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   862
      let
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   863
        val (checks, trans) = add_check_transform (name, def);
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   864
        fun check (check_defs, checker) modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   865
          let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   866
            fun get_def' s =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   867
              if NameSpace.is_qualified s
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   868
              then get_def modl s
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   869
              else Nop
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   870
            val defs =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   871
              check_defs
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   872
              |> map get_def';
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   873
          in
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   874
            case checker defs
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   875
             of NONE => modl
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   876
              | SOME msg => raise FAIL ([msg], NONE)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   877
          end;
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   878
        fun transform (name, f) modl =
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   879
          modl
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   880
          |> debug 9 (fn _ => "transforming node " ^ name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   881
          |> (if NameSpace.is_qualified name then map_def name f else I);
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   882
      in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   883
        modl
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   884
        |> debug 10 (fn _ => "considering addition of " ^ name
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   885
             ^ " := " ^ (Pretty.output o pretty_def) def)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   886
        |> debug 10 (fn _ => "consistency checks")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   887
        |> fold check checks
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   888
        |> debug 10 (fn _ => "dependencies")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   889
        |> fold (curry add_dep name) deps
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   890
        |> debug 10 (fn _ => "adding")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   891
        |> map_def name (fn _ => def)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   892
        |> debug 10 (fn _ => "transforming")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   893
        |> fold transform trans
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   894
        |> debug 10 (fn _ => "adding done")
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   895
      end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   896
    fun ensure_node name modl =
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   897
      (debug 9 (fn _ => "testing node " ^ quote name) ();
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   898
      if can (get_def modl) name
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   899
      then
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   900
        modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   901
        |> debug 9 (fn _ => "asserting node " ^ quote name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   902
        |> pair [name]
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   903
      else
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   904
        modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   905
        |> debug 9 (fn _ => "creating node " ^ quote name)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   906
        |> add_def (name, Nop)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   907
        |> debug 9 (fn _ => "checking creation of node " ^ quote name)
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   908
        |> select_generator (fn gname => "trying code generator " ^ gname ^ " for definition of " ^ quote name)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   909
             name defgens
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
   910
        |> debug 9 (fn _ => "checking creation of node " ^ quote name)
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   911
        |> check_fail msg
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   912
        |-> (fn (def, names') =>
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   913
           add (name, def)
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   914
           #> fold_map ensure_node names')
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   915
        |-> (fn names' => pair (name :: Library.flat names'))
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   916
      )
18170
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   917
  in
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   918
    modl
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   919
    |> ensure_node name
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   920
    |-> (fn names => pair (names@deps))
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   921
  end;
73ce773f12de added module system
haftmann
parents: 18169
diff changeset
   922
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   923
fun start_transact f modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   924
  let
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   925
    fun handle_fail f modl =
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   926
      ((([], modl) |> f)
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   927
      handle FAIL (msgs, NONE) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   928
        (error o cat_lines) ("code generation failed, while:" :: msgs))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   929
      handle FAIL (msgs, SOME e) =>
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   930
        ((writeln o cat_lines) ("code generation failed, while:" :: msgs); raise e);
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   931
  in
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   932
    modl
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   933
    |> handle_fail f
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   934
    |-> (fn x => fn (_, module) => (x, module))
2eea98bbf650 improved failure tracking
haftmann
parents: 18216
diff changeset
   935
  end;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   936
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   937
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   938
(** primitive language constructs **)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   939
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   940
val class_eq = "Eq"; (*defined for all primitve types and extensionally for all datatypes*)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   941
val type_bool = "Bool";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   942
val type_integer = "Integer"; (*infinite!*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   943
val type_float = "Float";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   944
val type_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   945
val type_list = "List";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   946
val cons_true = "True";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   947
val cons_false = "False";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   948
val cons_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   949
val cons_pair = "Pair";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   950
val cons_nil = "Nil";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   951
val cons_cons = "Cons";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   952
val fun_eq = "eq"; (*to class eq*)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   953
val fun_not = "not";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   954
val fun_and = "and";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   955
val fun_or = "or";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   956
val fun_if = "if";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   957
val fun_fst = "fst";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   958
val fun_snd = "snd";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   959
val fun_add = "add";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   960
val fun_mult = "mult";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   961
val fun_minus = "minus";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   962
val fun_lt = "lt";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   963
val fun_le = "le";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   964
val fun_wfrec = "wfrec";
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   965
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   966
local
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   967
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   968
val A = IVarT ("a", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   969
val B = IVarT ("b", []);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   970
val E = IVarT ("e", [class_eq]);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   971
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   972
in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   973
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   974
val Type_bool = type_bool `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   975
val Type_integer = type_integer `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   976
val Type_float = type_float `%% [];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   977
fun Type_pair a b = type_pair `%% [a, b];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   978
fun Type_list a = type_list `%% [a];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   979
val Cons_true = IConst (cons_true, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   980
val Cons_false = IConst (cons_false, Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   981
val Cons_pair = IConst (cons_pair, A `-> B `-> Type_pair A B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   982
val Cons_nil = IConst (cons_nil, Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   983
val Cons_cons = IConst (cons_cons, A `-> Type_list A `-> Type_list A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   984
val Fun_eq = IConst (fun_eq, E `-> E `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   985
val Fun_not = IConst (fun_not, Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   986
val Fun_and = IConst (fun_and, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   987
val Fun_or = IConst (fun_or, Type_bool `-> Type_bool `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   988
val Fun_if = IConst (fun_if, Type_bool `-> A `-> A `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   989
val Fun_fst = IConst (fun_fst, Type_pair A B `-> A);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   990
val Fun_snd = IConst (fun_snd, Type_pair A B `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   991
val Fun_0 = IConst ("0", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   992
val Fun_1 = IConst ("1", Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   993
val Fun_add = IConst (fun_add, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   994
val Fun_mult = IConst (fun_mult, Type_integer `-> Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   995
val Fun_minus = IConst (fun_minus, Type_integer `-> Type_integer);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   996
val Fun_lt = IConst (fun_lt, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   997
val Fun_le = IConst (fun_le, Type_integer `-> Type_integer `-> Type_bool);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   998
val Fun_wfrec = IConst (fun_wfrec, ((A `-> B) `-> A `-> B) `-> A `-> B);
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
   999
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1000
fun foldl1 f (x::xs) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1001
  Library.foldl f (x, xs);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1002
val ** = foldl1 (uncurry Type_pair);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1003
val XXp = foldl1 (fn (a, b) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1004
  let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1005
    val ty_a = itype_of_ipat a;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1006
    val ty_b = itype_of_ipat b;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1007
  in ICons ((cons_pair, [a, b]), Type_pair ty_a ty_b) end);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1008
val XXe = foldl1 (fn (a, b) =>
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1009
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1010
    val ty_a = itype_of_iexpr a;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1011
    val ty_b = itype_of_iexpr b;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1012
  in IConst (cons_pair, ty_a `-> ty_b `-> Type_pair ty_a ty_b) `$ a `$ b end);
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1013
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1014
end; (* local *)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1015
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1016
val prims = [class_eq, type_bool, type_integer, type_float, type_pair, type_list,
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1017
  cons_true, cons_false, cons_pair, cons_nil, cons_cons, fun_eq, fun_not, fun_and,
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1018
  fun_or, fun_if, fun_fst, fun_snd, fun_add, fun_mult, fun_minus, fun_lt, fun_le, fun_wfrec];
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1019
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1020
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1021
(** equality handling **)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1022
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1023
fun invoke_eq gen_ty gen_eq x (trns as (_ , modl)) =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1024
  let
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1025
    fun mk_eqpred dtname =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1026
      let
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1027
        val (vs, cons, _) = case get_def modl dtname of Datatype info => info;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1028
        val arity = map (rpair [class_eq] o fst) vs
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1029
        val ty = IType (dtname, map IVarT arity);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1030
        fun mk_eq (c, []) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1031
              ([ICons ((c, []), ty), ICons ((c, []), ty)], Cons_true)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1032
          | mk_eq (c, tys) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1033
              let
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1034
                val vars1 = Term.invent_names [] "a" (length tys);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1035
                val vars2 = Term.invent_names vars1 "b" (length tys);
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1036
                fun mk_eq_cons ty' (v1, v2) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1037
                  IConst (fun_eq, ty' `-> ty' `-> Type_bool) `$ IVarE (v1, ty) `$ IVarE (v2, ty)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1038
                fun mk_conj (e1, e2) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1039
                  Fun_and `$ e1 `$ e2;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1040
              in
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1041
                ([ICons ((c, map2 (curry IVarP) vars1 tys), ty),
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1042
                  ICons ((c, map2 (curry IVarP) vars2 tys), ty)],
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1043
                  foldr1 mk_conj (map2 mk_eq_cons tys (vars1 ~~ vars2)))
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1044
              end;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1045
        val eqs = map mk_eq cons @ [([IVarP ("_", ty), IVarP ("_", ty)], Cons_false)];
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1046
      in
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1047
        (Fun (eqs, (arity, ty `-> ty `-> Type_bool)), arity)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1048
      end;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1049
    fun invoke' (IType (tyco, tys)) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1050
          trns
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1051
          |> fold_map invoke' tys
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1052
          |-> (fn is_eq =>
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1053
                if forall I is_eq
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1054
                  then if NameSpace.is_qualified tyco
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1055
                  then
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1056
                    gen_eq (tyco, mk_eqpred tyco)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1057
                    #> pair true
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1058
                  else
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1059
                    pair true
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1060
                else
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1061
                  pair false)
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1062
      | invoke' (IFun _) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1063
          trns 
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1064
          |> pair false
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1065
      | invoke' (IVarT (_, sort)) trns =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1066
          trns 
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1067
          |> pair (member (op =) sort class_eq)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1068
  in
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1069
    trns
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1070
    |> gen_ty x
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1071
    |-> (fn ty => invoke' ty)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1072
  end;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1073
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1074
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1075
(** generic transformation **)
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1076
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1077
fun extract_defs e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1078
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1079
    fun extr_itype (ty as IType (tyco, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1080
          cons tyco #> fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1081
      | extr_itype ty =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1082
          fold_itype extr_itype ty
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1083
    fun extr_ipat (p as ICons ((c, _), _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1084
          cons c #> fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1085
      | extr_ipat p =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1086
          fold_ipat extr_itype extr_ipat p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1087
    fun extr_iexpr (e as IConst (f, _)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1088
          cons f #> fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1089
      | extr_iexpr e =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1090
          fold_iexpr extr_itype extr_ipat extr_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1091
  in extr_iexpr e [] end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1092
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1093
fun eta_expand query =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1094
  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1095
    fun eta_app ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1096
      let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1097
        val delta = query f - length es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1098
        val add_n = if delta < 0 then 0 else delta;
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1099
        val tys =
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1100
          (fst o unfold_fun) ty
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1101
          |> curry Library.drop (length es)
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1102
          |> curry Library.take add_n
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1103
        val add_vars =
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1104
          Term.invent_names (vars_of_iexprs es) "x" add_n ~~ tys;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1105
      in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1106
        Library.foldr IAbs (add_vars, IConst (f, ty) `$$ es `$$ (map IVarE add_vars))
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1107
      end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1108
    fun eta_iexpr' e = map_iexpr I I eta_iexpr e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1109
    and eta_iexpr (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1110
          eta_app ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1111
      | eta_iexpr (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1112
          (case (unfold_app e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1113
           of (IConst (f, ty), es) =>
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1114
                eta_app ((f, ty), map eta_iexpr es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1115
            | _ => eta_iexpr' e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1116
      | eta_iexpr e = eta_iexpr' e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1117
  in map_defs (map_def_fun I eta_iexpr) end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1118
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1119
val eta_expand_poly =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1120
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1121
    fun map_def_fun (def as Fun ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1122
          if (not o null) sortctxt
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1123
            orelse (null o tvars_of_itypes) [ty]
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1124
          then def
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1125
          else
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1126
            let
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
  1127
              val add_var = (hd (Term.invent_names (vars_of_iexprs [e]) "x" 1), ty1)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1128
            in (Fun ([([IVarP add_var], IAbs (add_var, e))], cty)) end
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1129
      | map_def_fun def = def;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1130
  in map_defs map_def_fun end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1131
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1132
fun tupelize_cons module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1133
  let
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1134
    fun replace_cons (cons as (_, [])) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1135
          pair cons
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1136
      | replace_cons (cons as (_, [_])) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1137
          pair cons
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1138
      | replace_cons (con, tys) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1139
          cons con
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1140
          #> pair (con, [** tys])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1141
    fun replace_def (_, (def as Datatype (vs, cs, insts))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1142
          fold_map replace_cons cs
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1143
          #-> (fn cs => pair (Datatype (vs, cs, insts)))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1144
      | replace_def (_, def) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1145
          pair def
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1146
    fun replace_app cs ((f, ty), es) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1147
      if member (op =) cs f
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1148
      then
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1149
        let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1150
          val (tys, ty') = unfold_fun ty
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1151
        in IConst (f, ** tys `-> ty') `$ XXe es end
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1152
      else IConst (f, ty) `$$ es;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1153
    fun replace_iexpr cs (IConst (f, ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1154
          replace_app cs ((f, ty), [])
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1155
      | replace_iexpr cs (e as IApp _) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1156
          (case unfold_app e
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1157
           of (IConst fty, es) => replace_app cs (fty, map (replace_iexpr cs) es)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1158
            | _ => map_iexpr I I (replace_iexpr cs) e)
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1159
      | replace_iexpr cs e = map_iexpr I I (replace_iexpr cs) e;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1160
    fun replace_ipat cs (p as ICons ((c, ps), ty)) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1161
          if member (op =) cs c then
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1162
            ICons ((c, [XXp (map (replace_ipat cs) ps)]), ty)
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1163
          else map_ipat I (replace_ipat cs) p
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1164
      | replace_ipat cs p = map_ipat I (replace_ipat cs) p;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1165
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1166
    transform_defs replace_def replace_ipat replace_iexpr [cons_cons] module
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1167
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1168
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1169
fun eliminate_classes module =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1170
  let
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1171
    fun transform_itype (IVarT (v, s)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1172
          IVarT (v, [])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1173
      | transform_itype (ty as IDictT _) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1174
          ty
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1175
      | transform_itype ty =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1176
          map_itype transform_itype ty;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1177
    fun transform_ipat p =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1178
          map_ipat transform_itype transform_ipat p;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1179
    fun transform_iexpr vname_alist (IInst (e, ls)) =
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1180
          let
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1181
            fun transform_lookup (ClassPackage.Instance ((cdict, idict), ls)) =
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1182
                  ls
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1183
                  |> transform_lookups
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1184
                  |-> (fn tys =>
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1185
                        curry mk_apps (IConst (idict, cdict `%% tys))
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1186
                        #> pair (cdict `%% tys))
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1187
              | transform_lookup (ClassPackage.Lookup (deriv, (v, i))) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1188
                  let
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1189
                    val (v', cls) =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1190
                      (nth o the oo AList.lookup (op =)) vname_alist v i;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1191
                    fun mk_parm tyco = tyco `%% [IVarT (v, [])];
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1192
                  in (mk_parm cls, ILookup (deriv, v')) end
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1193
            and transform_lookups lss =
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1194
                  map_yield (map_yield transform_lookup
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1195
                       #> apfst **
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1196
                       #> apsnd XXe) lss
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1197
          in transform_iexpr vname_alist e `$$ (snd o transform_lookups) ls end
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1198
      | transform_iexpr vname_alist e =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1199
          map_iexpr transform_itype transform_ipat (transform_iexpr vname_alist) e;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1200
    fun mk_cls_typ_map v membrs ty_inst =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1201
      map (fn (m, (mctxt, ty)) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1202
        (m, ty |> instant_itype (v, ty_inst))) membrs;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1203
    fun extract_members (cls, Class (supclss, v, membrs, _)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1204
          let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1205
            val ty_cls = cls `%% [IVarT (v, [])];
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1206
            val w = "d";
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1207
            val add_supclss = if null supclss then I else cons (v, supclss);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1208
            fun mk_fun (m, (mctxt, ty)) = (m, Fun ([([IVarP (w, ty_cls)], ILookup ([m], w))],
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1209
              (add_supclss mctxt, ty `-> ty_cls)));
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1210
          in fold (cons o mk_fun) membrs end
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1211
      | extract_members _ = I;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1212
    fun introduce_dicts (Class (supcls, v, membrs, insts)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1213
          let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1214
            val varname_cls = Term.invent_names (tvars_of_itypes (map (snd o snd) membrs)) "a" 1 |> hd
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1215
          in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1216
            Typesyn ([(varname_cls, supcls)], IDictT (mk_cls_typ_map v membrs (IVarT (varname_cls, []))))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1217
          end
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1218
      | introduce_dicts (Classinst (clsname, (tyco, arity), memdefs)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1219
          let
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1220
            val Class (_, v, members, _) =
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1221
              if clsname = class_eq
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1222
              then
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1223
                Class ([], "a", [(fun_eq, ([], IVarT ("a", []) `-> IVarT ("a", []) `-> Type_bool))], [])
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1224
              else
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
  1225
                get_def module clsname;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1226
            val ty = tyco `%% map IVarT arity;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1227
            val inst_typ_map = mk_cls_typ_map v members ty;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1228
            val memdefs_ty = map (fn (memname, memprim) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1229
              (memname, (memprim, (the o AList.lookup (op =) inst_typ_map) memname))) memdefs;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1230
          in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1231
            Fun ([([], IDictE (map (apsnd IConst) memdefs_ty))],
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1232
              ([], IType (clsname, [ty])))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1233
          end
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1234
      | introduce_dicts d = d;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1235
    fun elim_sorts (Fun (eqs, ([], ty))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1236
          Fun (map (fn (ps, rhs) => (map transform_ipat ps, transform_iexpr [] rhs)) eqs,
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1237
            ([], transform_itype ty))
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1238
      | elim_sorts (Fun (eqs, (sortctxt, ty))) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1239
          let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1240
            val varnames_ctxt =
18441
7488d8ea61bc removed infix prefix, introduces burrow
haftmann
parents: 18385
diff changeset
  1241
              burrow
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1242
                (Term.invent_names ((vars_of_iexprs o map snd) eqs @
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1243
                  (vars_of_ipats o Library.flat o map fst) eqs) "d" o length)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1244
                (map snd sortctxt);
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1245
            val vname_alist = map2 (fn (vt, sort) => fn vs => (vt, vs ~~ sort))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1246
              sortctxt varnames_ctxt;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1247
            val ty' = map (op ** o (fn (vt, vss) => map (fn (_, cls) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1248
              cls `%% [IVarT (vt, [])]) vss)) vname_alist
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1249
              `--> transform_itype ty;
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1250
            val ps_add = map (XXp o (fn (vt, vss) => map (fn (v, cls) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1251
              IVarP (v, cls `%% [IVarT (vt, [])])) vss)) vname_alist;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1252
          in Fun (map (fn (ps, rhs) => (ps_add @ map transform_ipat ps, transform_iexpr vname_alist rhs)) eqs, ([], ty')) end
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1253
      | elim_sorts (Datatype (vars, constrs, insts)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1254
          Datatype (map (fn (v, _) => (v, [])) vars, map (apsnd (map transform_itype)) constrs, insts)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1255
      | elim_sorts (Typesyn (vars, ty)) =
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1256
          Typesyn (map (fn (v, _) => (v, [])) vars, transform_itype ty)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1257
      | elim_sorts d = d;
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1258
  in
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1259
    module
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1260
    |> `(fn module => fold_defs extract_members module [])
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1261
    |-> (fn membrs => fold (fn (name, f) => map_def name (K f)) membrs)
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1262
    |> map_defs introduce_dicts
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1263
    |> map_defs elim_sorts
18172
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1264
  end;
8ff5bcfae27a added generic transformators
haftmann
parents: 18170
diff changeset
  1265
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1266
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1267
(** generic serialization **)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1268
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1269
(* resolving *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1270
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1271
fun mk_resolvtab nspgrp validate module =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1272
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1273
    fun ensure_unique prfix prfix' name name' (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1274
      let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1275
        fun uniquify name n =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1276
          let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1277
            val name' = if n = 0 then name else name ^ "_" ^ string_of_int n
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1278
          in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1279
            if member (op =) locals name'
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1280
            then uniquify name (n+1)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1281
            else case validate name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1282
              of NONE => name'
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1283
               | SOME name' => uniquify name' n
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1284
          end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1285
        val name'' = uniquify name' 0;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1286
      in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1287
        (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1288
        |> apsnd (Symtab.update_new
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1289
             (NameSpace.pack (prfix @ [name]), NameSpace.pack (prfix' @ [name''])))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1290
        |> apfst (cons name'')
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1291
        |> pair name''
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1292
      end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1293
    fun fill_in prfix prfix' node tab =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1294
      let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1295
        val keys = Graph.keys node;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1296
        val nodes = AList.make (Graph.get_node node) keys;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1297
        val (mods, defs) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1298
          nodes
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1299
          |> List.partition (fn (_, Module _) => true | _ => false)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1300
          |> apfst (map (fn (name, Module m) => (name, m)))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1301
          |> apsnd (map fst)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1302
        fun modl_validate (name, modl) (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1303
          (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1304
          |> ensure_unique prfix prfix' name name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1305
          |-> (fn name' => apsnd (fill_in (prfix @ [name]) (prfix @ [name']) modl))
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1306
        fun ensure_unique_sidf sidf =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1307
          let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1308
            val [shallow, name] = NameSpace.unpack sidf;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1309
          in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1310
            nspgrp
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1311
            |> get_first
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1312
                (fn grp => if member (op =) grp shallow
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1313
                  then grp |> remove (op =) shallow |> SOME else NONE)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1314
            |> these
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1315
            |> map (fn s => NameSpace.pack [s, name])
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1316
            |> exists (member (op =) defs)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1317
            |> (fn b => if b then sidf else name)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1318
          end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1319
        fun def_validate sidf (locals, tab) =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1320
          (locals, tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1321
          |> ensure_unique prfix prfix' sidf (ensure_unique_sidf sidf)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1322
          |> snd
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1323
      in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1324
        ([], tab)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1325
        |> fold modl_validate mods
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1326
        |> fold def_validate defs
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1327
        |> snd
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1328
      end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1329
  in
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1330
    Symtab.empty
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1331
    |> fill_in [] [] module
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1332
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1333
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1334
fun mk_resolv tab =
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1335
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1336
    fun resolver modl name =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1337
      if NameSpace.is_qualified name then
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1338
        let
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1339
          val _ = debug 12 (fn name' => "resolving " ^ quote name ^ " in " ^ (quote o NameSpace.pack) modl) ();
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1340
          val modl' = if null modl then [] else (NameSpace.unpack o the o Symtab.lookup tab o NameSpace.pack) modl;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1341
          val name' = (NameSpace.unpack o the o Symtab.lookup tab) name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1342
        in
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1343
          (NameSpace.pack o snd o snd o get_prefix (op =)) (modl', name')
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1344
          |> debug 12 (fn name' => "resolving " ^ quote name ^ " to " ^ quote name' ^ " in " ^ (quote o NameSpace.pack) modl)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1345
        end
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1346
      else name
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1347
  in resolver end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1348
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1349
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1350
(* serialization *)
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1351
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1352
fun serialize s_def s_module validate nspgrp name_root module =
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1353
  let
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1354
    val resolvtab = mk_resolvtab nspgrp validate module;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1355
    val resolver = mk_resolv resolvtab;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1356
    fun seri prfx ([(name, Module module)]) =
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
  1357
          s_module (resolver prfx (prfx @ [name] |> NameSpace.pack),
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1358
            List.mapPartial (seri (prfx @ [name]))
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1359
              ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1360
          |> SOME
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1361
      | seri prfx ds =
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1362
          s_def (resolver prfx) (map
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18441
diff changeset
  1363
            (fn (name, Def def) => (resolver prfx (prfx @ [name] |> NameSpace.pack), def)) ds)
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1364
  in
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
  1365
    setmp print_mode [] (fn _ => s_module (name_root, (List.mapPartial (seri [])
18361
3126d01e9e35 improved serialization of classes to haskell
haftmann
parents: 18360
diff changeset
  1366
      ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)))) ()
18216
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1367
  end;
db7d43b25c99 added serializer
haftmann
parents: 18172
diff changeset
  1368
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1369
end; (* struct *)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1370
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1371
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1372
structure CodegenThingol : CODEGEN_THINGOL =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1373
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1374
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1375
open CodegenThingolOp;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1376
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
  1377
end; (* struct *)