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