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